import imp
from locale import ABDAY_1
from re import A
from learn_sql import db,chengji,student,course  #导入实例化的数据库和类

# 增
# a1 = student(name='清欢')   
# a2 =student(name='qinghuan') 
# a3 =student(name='qq')
# a4 =student(name='q2')
# a5 =student(name='asf')
# # a为任意变量名
# # db.session.add(a)   #add添加
# # db.session.commit()   #commit提交
# #第二种添加方法，（）为表
# db.session.add_all([a1,a2,a3,a4,a5])
# db.session.commit()

# 查询数据库,b为自定义变量,get内关键词为表的主键
# b = student.query.get(1)
# print(b.name)

#查询所有
# c = student.query.all()
# #for循环，打印
# for i in c:
#     print(i.name)

# 条件查询，filter
# d = student.query.filter(student.id >=5)
# for i in d:
#     print(i.id,i.name)
# 条件查询，filter_by()，括号内为条件,当有多个相同值时可以用 .first()显示第一个
# e = student.query.filter_by(name='qinghuan').all()
# for i in e:
#     print(i.id,i.name)

# 改
# 第一种改法
# e = student.query.filter(student.name == 'qinghuan').update({"name":'张三'})        #打印变量，返回的是改了多少条数据
# db.session.commit()
# print(e)        #此处返回的是改了多少条数据

# 第二种改法
# f = student.query.filter(student.name =='清欢').all()
# for i in f:
#     i.name ="xxxxx"
# db.session.add(i) 
# db.session.commit()

# 删除
# g = student.query.filter(student.name == '清欢').delete()
# db.session.commit()

# chengji_1 = chengji(student_id =1,chengji = 100)
# chengji_2 = chengji(student_id =1,chengji = 60)
# chengji_3 = chengji(student_id =2,chengji = 70)
# db.session.add_all([chengji_1,chengji_2,chengji_3])
# db.session.commit()

kc1 = course(name="语文")
kc2 = course(name="数学")
kc3 = course(name="英语")
kc4 = course(name="体育")
db.session.add_all([kc1,kc2,kc3,kc4])
db.session.commit()
